<?php

namespace common\modules\user\controllers\backend;

use backend\components\AdminModule as Module;
use common\components\AdminController;
use common\modules\user\models\backend\RuleSearch;
use Yii;
use yii\filters\VerbFilter;
use yii\web\HttpException;

/**
 * Rules controller.
 */
class RuleController extends AdminController
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = [];

        $behaviors['verbs'] = [
            'class'   => VerbFilter::className(),
            'actions' => [
                'index'        => ['get'],
                'create'       => ['get', 'post'],
                'delete'       => ['post', 'delete'],
                'batch-delete' => ['post', 'delete'],
            ],
        ];

        return $behaviors;
    }

    /**
     * Rules list page.
     */
    public function actionIndex()
    {
        $searchModel = new RuleSearch;
        $dataProvider = $searchModel->search(Yii::$app->request->get());

        $page = $this->createGridViewPage($dataProvider, $searchModel);
        return $page->render();
    }

    /**
     * Find rule by name.
     *
     * @param string $name Rule name
     *
     * @return \yii\rbac\Rule Rule
     *
     * @throws HttpException 404 error if rule not found
     */
    protected function findRule($name)
    {
        if (($model = Yii::$app->authManager->getRule($name)) !== null)
        {
            return $model;
        }
        else
        {
            throw new HttpException(404, Module::t('user', 'BACKEND_RULES_NOT_FOUND'));
        }
    }
}
